package com.doujiang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doujiang.domain.MovieTag;
import com.doujiang.domain.Tag;
import com.doujiang.mapper.MovieTagMapper;
import com.doujiang.service.IMovieTagService;
import com.doujiang.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 电影标签关联管理Service业务层处理
 * 
 * @author 王伟舰
 * @date 2025-07-19
 */
@Service
public class MovieTagServiceImpl extends ServiceImpl<MovieTagMapper, MovieTag> implements IMovieTagService
{
    @Autowired
    private ITagService tagService;

    @Override
    public List<Tag> getTagsByMovieId(Long movieId) {
        // 获取电影的标签ID列表
        List<Long> tagIds = lambdaQuery()
            .eq(MovieTag::getMovieId, movieId)
            .list()
            .stream()
            .map(MovieTag::getTagId)
            .collect(Collectors.toList());
        
        if (tagIds.isEmpty()) {
            return List.of();
        }
        
        // 根据标签ID列表获取标签实体
        return tagService.listByIds(tagIds);
    }

    @Override
    public List<Long> getMovieIdsByTagId(Long tagId) {
        return lambdaQuery()
            .eq(MovieTag::getTagId, tagId)
            .list()
            .stream()
            .map(MovieTag::getMovieId)
            .collect(Collectors.toList());
    }

    @Override
    public List<Long> getMovieIdsByTagIds(List<Long> tagIds) {
        if (tagIds == null || tagIds.isEmpty()) {
            return List.of();
        }
        
        return lambdaQuery()
            .in(MovieTag::getTagId, tagIds)
            .list()
            .stream()
            .map(MovieTag::getMovieId)
            .distinct()
            .collect(Collectors.toList());
    }

    @Override
    public List<Long> getTagIdsByMovieId(Long movieId) {
        return lambdaQuery()
            .eq(MovieTag::getMovieId, movieId)
            .list()
            .stream()
            .map(MovieTag::getTagId)
            .collect(Collectors.toList());
    }
}
